﻿using System.Collections;
using CTS.Com.Domain.Exceptions;
using CTS.Com.Domain.Helper;
using CTS.Com.Domain.Validate.Model;

namespace CTS.Com.Domain.Validate.Attr
{
    public class ListValidation : AbstractValidation
    {
        public bool MsgHideRowNo { get; set; }

        public override void IsValid(object item, Rule rule)
        {
            // Convert value to list
            var list = (IList)item;
            // Check required
            if (Required && DataCheckHelper.IsNull(list)) {
                throw new ExecuteException("E_MSG_00001", MessageParam);
            }
            // Browse by object
            var i = 1;
            foreach (var obj in list) {
                // Recursive
                try {
                    ValidateHelper.Validate(obj);
                } catch (SysRuntimeException ex) {
                    if (MsgHideRowNo) {
                        throw new ExecuteException("E_MSG_00026", MessageParam, ex.Message.MessageDetail);
                    }
                    throw new ExecuteException("E_MSG_00010", MessageParam, i, ex.Message.MessageDetail);
                }
                i++;
            }
        }
    }
}
